package jblip.gui.data.images;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import jblip.base.HttpClient;
import jblip.gui.JBlip;
import jblip.resources.UserPicture;
import jblip.resources.UserPicture.PictureSize;

public class IconCache extends ImageCache {
  public IconCache(final File cache_dir) {
    super(new File(cache_dir.getPath() + File.separator + "avatars"));
  }

  public synchronized void getUserIcon(final String username,
      final PictureSize size, final ImageChangeListener receiver) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        final HttpClient client = JBlip.instance().getBlipClient();
        final UserPicture pic = client.getAvatar(username);

        if (pic != null) {
          try {
            final String url =
                String
                    .format("http://%s%s", client.getHost(), pic.getURL(size));
            getImage(new URL(url), receiver);
          } catch (MalformedURLException e) {
            // wut!? blip API is wrong?
            e.printStackTrace();
          }
        }

      }
    }).start();
  }
}
